<?php
require (Yii::app ()->basePath . '/lib/rolling-curl/RollingCurl.php');
/**
 *
 * @author 高维 2016.4.12 Create
 * @version V0.0.1
 * @see 系统管理控制器
 * @todo 管理员添加，列表，删除，编辑等功能
 * @copyright © 2015 Huxun. All Rights Reserved
 */
class SysController extends CController {
	
	/**
	 *
	 * @see 数据字典
	 */
	function actionDict_type() {
		$this->render ( 'dict_type' );
	}
	/**
	 *
	 * @see 权限管理
	 */
	function actionRoot() {
		$id = $_GET ['id'];
		$service = new SysAdminEntity ();
		$model = $service->get ( $id );
		sys_debug_log ( $model );
		if (false === $model) {
			exit ( "参数错误" );
		}
		$this->render ( 'root', array (
				'model' => $model 
		) );
	}
	
	/**
	 *
	 * @see 获取所有数据字典
	 */
	function actionAjax_dict_type_list() {
		$ret = new stdClass ();
		$ret->status = 401;
		/*
		 * if (Yii::app ()->user->isGuest) {
		 * $ret->data = "请登录";
		 * echo json_encode ( $ret );
		 * Yii::app ()->end ();
		 * }
		 */
		$service = new SysDictionaryTypeEntity ();
		$list = $service->get_list ( - 1, - 1, array (
				'deleted' => 0 
		), array (
				'id' => 'desc' 
		) );
		if ($list === false) {
			$ret->status = 0;
			$ret->data = "未找到匹配数据";
		} else {
			$ret->status = 1;
			$ret->data = $list;
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see 数据字典列表
	 *
	 */
	function actionDict_list() {
		$dt_pk = $_GET ['dt_pk'];
		$this->render ( "dict", array (
				'dt_pk' => $dt_pk 
		) );
	}
	
	/**
	 *
	 * @see ajax 上移
	 */
	function actionAjax_dict_up() {
		$ret = new stdClass ();
		$ret->status = 500;
		/*
		 * if (Yii::app ()->user->isGuest) {
		 * $ret->data = "请登录";
		 * echo json_encode ( $ret );
		 * Yii::app ()->end ();
		 * }
		 */
		if (isset ( $_POST ['id'] ) && is_numeric ( $_POST ['id'] )) {
			$id = $_POST ['id'];
			$service = new SysDictionaryEntity ();
			$model = $service->get ( $id );
			if (is_array ( $model ) && count ( $model ) > 0) {
				$ct = $service->get_count ( array (
						'sys_dict_type_id' => $model ['sys_dict_type_id'],
						'deleted' => 0 
				) );
				sys_debug_log ( "$model 变量====>" . $model );
				if (false === $ct) {
					$ct = 1;
				}
				sys_debug_log ( "ct 变量====>" . $ct );
				if ($model ['sys_order_num'] < $ct) {
					$ct = $model ['sys_order_num'] + 1;
				}
				$result = $service->update ( array (
						'sys_order_num' => $ct 
				), array (
						'id' => $id 
				) );
				if ($result) {
					$ret->status = 200;
				}
			}
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see ajax下移
	 *
	 */
	function actionAjax_dict_down() {
		$ret = new stdClass ();
		$ret->status = 500;
		/*
		 * if (Yii::app ()->user->isGuest) {
		 * $ret->data = "请登录";
		 * echo json_encode ( $ret );
		 * Yii::app ()->end ();
		 * }
		 */
		if (isset ( $_POST ['id'] ) && is_numeric ( $_POST ['id'] )) {
			$id = $_POST ['id'];
			$service = new SysDictionaryEntity ();
			$model = $service->get ( $id );
			if (is_array ( $model ) && count ( $model ) > 0) {
				$ct = $model ['sys_order_num'] - 1;
				if ($model ['sys_order_num'] == 1) {
					$ct = 1;
				}
				sys_debug_log ( "ct 变量====>" . $ct );
				$result = $service->update ( array (
						'sys_order_num' => $ct 
				), array (
						'id' => $id 
				) );
				if ($result) {
					$ret->status = 200;
				}
			}
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see ajax 删除产品分类
	 */
	function actionAjax_dict_delete() {
		$ret = new stdClass ();
		$ret->status = 500;
		/*
		 * if (Yii::app ()->user->isGuest) {
		 * $ret->data = "请登录";
		 * echo json_encode ( $ret );
		 * Yii::app ()->end ();
		 * }
		 */
		if (isset ( $_POST ['id'] ) && is_numeric ( $_POST ['id'] )) {
			$id = $_POST ['id'];
			$service = new SysDictionaryEntity ();
			$result = $service->delete_bogus ( $id );
			if ($result) {
				$ret->status = 200;
				$ret->data = "操作成功";
			} else {
				$ret->data = "操作失败";
			}
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see 数据字典列表
	 */
	function actionAjax_dict_list() {
		$ret = new stdClass ();
		$ret->status = 0;
		/*
		 * if (Yii::app ()->user->isGuest) {
		 * $ret->data = "请登录";
		 * echo json_encode ( $ret );
		 * Yii::app ()->end ();
		 * }
		 */
		$service = new SysDictionaryEntity ();
		$pg = $_GET ['page'];
		$dt_pk = $_GET ['dt_pk'];
		// 默认刷新页面
		if (empty ( $pg ) || ! is_numeric ( $pg ) || $pg <= 0)
			$pg = 1;
		$list = $service->get_list ( $pg, 10, array (
				'sys_dict_type_id' => $dt_pk,
				'deleted' => 0 
		), array (
				'sys_order_num' => 'desc' 
		) );
		$totalCount = $service->get_count ( array (
				'sys_dict_type_id' => $dt_pk,
				'deleted' => 0 
		) );
		$page_count = 1;
		if ($totalCount > 0)
			$page_count = ceil ( $totalCount / 10.0 );
		if ($list === false) {
			$ret->status = 0;
			$ret->data = "未找到匹配数据";
		} else {
			$ret->status = 1;
			$ret->data = $list;
			$ret->page_count = $page_count;
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see 添加/编辑数据字典子项
	 */
	function actionDict_editor() {
		// 获取字典ID,判断是编辑还是新增
		$d_id = $_GET ['did'] or "";
		if (is_numeric ( $d_id ) && $d_id > 0) {
			// 存在ID,查询原始信息
			$dict_service = new SysDictionaryEntity ();
			$dict_model = $dict_service->get ( $d_id );
			if (false === $model) {
				sys_debug_log ( "did参数错误" );
				echo "500参数错误";
				return;
			}
		}
		// 参数必须传值，获取字典类型信息
		$dt_pk = $_GET ['dt_pk'];
		if (is_numeric ( $dt_pk ) && $dt_pk > 0) {
			$service = new SysDictionaryTypeEntity ();
			$model = $service->get ( $dt_pk );
			fb ( $model );
			if (false === model) {
				// 错误请求
				echo "500参数错误";
				return;
			}
			$this->render ( "dict_editor", array (
					'dt_pk' => $dt_pk,
					'model' => $model,
					'd_model' => $dict_model 
			) );
		} else {
			sys_debug_log ( "dt_pk参数错误" );
			// 错误请求
			echo "500参数错误";
			return;
		}
	}
	
	/**
	 *
	 * @see 编辑数据字段
	 */
	function actionAjax_dict_editor() {
		$ret = new stdClass ();
		$ret->status = 500;
		$ret->data = "请求参数错误";
		$id = $_POST ['id'];
		$dt_pk = $_POST ['dt_pk'] or "";
		$sys_value = $_POST ['dt_value'] or "";
		if (! is_numeric ( $dt_pk ) || $dt_pk == "0") {
			sys_debug_log ( "dt_pk参数错误" );
			$ret->status = 500;
			$ret->data = "请求参数错误";
			echo json_encode ( $ret );
			return;
		}
		if (is_null ( $sys_value ) || $sys_value == "") {
			sys_debug_log ( "sys_value参数错误" );
			$ret->status = 500;
			$ret->data = "请求参数错误";
			echo json_encode ( $ret );
			return;
		}
		$sys_order_number = $_POST ['sys_order_number'];
		$services = new SysDictionaryEntity ();
		if (is_numeric ( $id ) && $id > 0) {
			// 更新数据字典值
			$fld = array (
					'sys_dict_type_id' => $dt_pk,
					'sys_value' => $sys_value,
					'sys_order_num' => $sys_order_number 
			);
			$result = $services->update ( $fld, array (
					'id' => $id 
			) );
			if ($result === true) {
				$ret->status = 200;
				$ret->data = "操作成功";
			} else {
				$ret->status = 500;
				$ret->data = "操作失败";
			}
		} else {
			// 新建数据字典值
			// 更新数据字典值
			$fld = array (
					'sys_dict_type_id' => $dt_pk,
					'sys_value' => $sys_value,
					'sys_order_num' => $sys_order_number,
					'sys_create_dt' => date ( 'Y-m-d H:i:s', time () ),
					'deleted' => 0 
			);
			$result = $services->add ( $fld );
			if (is_numeric ( $result ) && $result > 0) {
				$ret->status = 200;
				$ret->data = "操作成功";
			} else {
				$ret->status = 500;
				$ret->data = "操作失败";
			}
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see 管理员列表视图
	 */
	function actionAdmin() {
		$this->render ( "admin" );
	}
	
	/**
	 *
	 * @see 管理员数据查询Action
	 */
	function actionAjax_admin_list() {
		$ret = new stdClass ();
		$ret->status = 0;
		/*
		 * if (Yii::app ()->user->isGuest) {
		 * $ret->data = "请登录";
		 * echo json_encode ( $ret );
		 * Yii::app ()->end ();
		 * }
		 */
		$service = new SysAdminEntity ();
		$pg = $_GET ['page'];
		// 默认刷新页面
		if (empty ( $pg ) || ! is_numeric ( $pg ) || $pg <= 0)
			$pg = 1;
		$adminList = $service->get_list ( $pg, 10, array (
				'deleted' => 0 
		), array (
				'id' => 'desc' 
		) );
		$totalCount = $service->get_count ( array (
				'deleted' => 0 
		) );
		$page_count = 1;
		if ($totalCount > 0)
			$page_count = ceil ( $totalCount / 10.0 );
		$ret = new stdClass ();
		$ret->pi = $pg;
		if ($adminList === false) {
			$ret->status = 0;
			$ret->data = "未找到匹配数据";
		} else {
			$dicService = new SysDictionaryEntity ();
			$posList = $dicService->get_list ( - 1, - 1, array (
					'sys_dict_type_id' => 1,
					'deleted' => 0 
			), array (
					"sys_order_num" => 'desc' 
			) );
			$depService = new SysDepartEntity ();
			$depList = $depService->get_list ( - 1, - 1, array (
					'deleted' => 0 
			), array (
					'id' => 'desc' 
			) );
			$index = 0;
			foreach ( $adminList as $item ) {
				$adminList [$index] ['depart'] = array_seach_multi ( $depList, 'id', $item ['depart_id'], 'depart_name' );
				$adminList [$index] ['position'] = array_seach_multi ( $posList, "id", $item ['position_id'], "sys_value" );
				$index ++;
			}
			$ret->status = 1;
			$ret->data = $adminList;
			$ret->page_count = $page_count;
		}
		echo json_encode ( $ret );
	}
	/**
	 *
	 * @see 用户管理-用户列表-详情
	 */
	public function actionAdmindetail() {
		if (isset ( $_GET ['id'] ) && is_numeric ( $_GET ['id'] )) {
			$id = $_GET ['id'];
			$service = new SysAdminEntity ();
			$fld = array (
					//'id',
					'account',
					'name',
					'phone',
					'(SELECT sys_value from sys_dictionary where id=position_id LIMIT 1) as position',
					//'sys_role',
					'(SELECT depart_name from sys_depart where id=depart_id LIMIT 1) as depart' 
			);
			$model = $service->get ( $id, $fld );
			if (false === $admin) {
				exit ( "参数错误" );
			} else {
				$this->renderPartial ( 'admindetail', array (
						'model' => $model 
				) );
			}
		} else {
			exit ( "参数错误" );
		}
	}
	/**
	 *
	 * @see 管理员编辑界面视图
	 */
	function actionAdmin_editor() {
		$id = $_GET ['id'];
		if (is_numeric ( $id ) && $id > 0) {
			$service = new SysAdminEntity ();
			$model = $service->get ( $id );
			if (false === $model) {
				echo "参数错误";
				return;
			}
		}
		$dicService = new SysDictionaryEntity ();
		$posList = $dicService->get_list ( - 1, - 1, array (
				'sys_dict_type_id' => 1,
				'deleted' => 0 
		), "sys_order_num" );
		$depService = new SysDepartEntity ();
		$depList = $depService->get_list ( - 1, - 1, array (
				'deleted' => 0 
		), 'id' );
		return $this->render ( "admin_editor", array (
				"model" => $model,
				'depList' => $depList,
				'posList' => $posList 
		) );
	}
	
	/**
	 *
	 * @see ajax 编辑管理员信息
	 */
	function actionAjax_admin_editor() {
		$ret = new stdClass ();
		$ret->status = 500;
		$id = $_POST ['id'];
		$account = $_POST ['account'];
		// 账号
		$pwd = $_POST ['pwd'];
		// 密码
		$name = $_POST ['name'];
		// 姓名
		$phone = $_POST ['phone'];
		// 联系电话
		$depart = $_POST ['depart'];
		$position = $_POST ['pos'];
		// 职位
		$service = new SysAdminEntity ();
		if (is_numeric ( $id ) && $id > 0) {
			$model = $service->get ( $id );
			if (false === $model) {
				$ret->status = 500;
				$ret->data = "参数错误";
				echo json_encode ( $ret );
				return;
			}
			$fld = array (
					'name' => $name,
					'phone' => $phone,
					'depart_id' => $depart,
					'position_id' => $position 
			);
			$result = $service->update ( $fld, array (
					'id' => $id 
			) );
			if (false === $result) {
				$ret->status = 500;
				$ret->data = "操作失败";
				echo json_encode ( $ret );
				return;
			} else {
				$ret->status = 200;
				$ret->data = "操作成功";
				echo json_encode ( $ret );
				return;
			}
		} else {
			$fld = array (
					'account' => $account,
					'password' => md5 ( $pwd ),
					'name' => $name,
					'phone' => $phone,
					'depart_id' => $depart,
					'position_id' => $position,
					'deleted' => 0 
			);
			$result = $service->add ( $fld );
			if (is_numeric ( $result ) && $result > 0) {
				$ret->status = 200;
				$ret->data = "操作成功";
				echo json_encode ( $ret );
				return;
			} else {
				$ret->status = 500;
				$ret->data = "操作失败";
				echo json_encode ( $ret );
				return;
			}
		}
	}
	
	/**
	 *
	 * @see ajax 删除管理员信息
	 */
	function actionAjax_admin_delete() {
		$ret = new stdClass ();
		$ret->status = 500;
		/*
		 * if (Yii::app ()->user->isGuest) {
		 * $ret->data = "请登录";
		 * echo json_encode ( $ret );
		 * Yii::app ()->end ();
		 * }
		 */
		if (isset ( $_POST ['id'] ) && is_numeric ( $_POST ['id'] )) {
			$id = $_POST ['id'];
			$service = new SysAdminEntity ();
			$result = $service->delete_bogus ( $id );
			if ($result) {
				$ret->status = 200;
				$ret->data = "操作成功";
			} else {
				$ret->data = "操作失败";
			}
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see 部门管理界面
	 */
	function actionDepart() {
		$this->render ( 'depart' );
	}
	
	/**
	 *
	 * @see Ajax 获取部门列表
	 */
	function actionAjax_depart_list() {
		$ret = new stdClass ();
		$ret->status = 0;
		/*
		 * if (Yii::app ()->user->isGuest) {
		 * $ret->data = "请登录";
		 * echo json_encode ( $ret );
		 * Yii::app ()->end ();
		 * }
		 */
		$service = new SysDepartEntity ();
		$pg = $_GET ['page'];
		// 默认刷新页面
		if (empty ( $pg ) || ! is_numeric ( $pg ) || $pg <= 0)
			$pg = 1;
		$list = $service->get_list ( $pg, 10, array (
				'deleted' => 0 
		), array (
				'id' => 'desc' 
		) );
		$totalCount = $service->get_count ( array (
				'deleted' => 0 
		) );
		$page_count = 1;
		if ($totalCount > 0)
			$page_count = ceil ( $totalCount / 10.0 );
		if ($list === false) {
			$ret->status = 0;
			$ret->data = "未找到匹配数据";
		} else {
			$ret->status = 1;
			$ret->data = $list;
			$ret->page_count = $page_count;
		}
		echo json_encode ( $ret );
	}
	
	/**
	 *
	 * @see 编辑部门界面
	 */
	function actionDepart_editor() {
		$id = $_GET ['id'];
		if (is_numeric ( $id ) && $id > 0) {
			$service = new SysDepartEntity ();
			$model = $service->get ( $id );
		}
		$this->render ( "depart_editor", array (
				'model' => $model 
		) );
	}
	
	/**
	 *
	 * @see ajax 编辑部门信息
	 */
	function actionAjax_depart_editor() {
		$ret = new stdClass ();
		$ret->status = 500;
		$id = $_POST ['id'];
		$name = $_POST ['name'];
		$service = new SysDepartEntity ();
		if (is_numeric ( $id ) && $id > 0) {
			$model = $service->get ( $id );
			if (false === $model) {
				$ret->status = 500;
				$ret->data = "参数错误";
				echo json_encode ( $ret );
				return;
			}
			$fld = array (
					"depart_name" => $name 
			);
			$result = $service->update ( $fld, array (
					'id' => $id 
			) );
			if (false === $result) {
				$ret->status = 500;
				$ret->data = "操作失败";
				echo json_encode ( $ret );
				return;
			} else {
				$ret->status = 200;
				$ret->data = "操作成功";
				echo json_encode ( $ret );
				return;
			}
		} else {
			$fld = array (
					"depart_name" => $name,
					'deleted' => 0 
			);
			$result = $service->add ( $fld );
			sys_debug_log ( $result );
			if (is_numeric ( $result ) && $result > 0) {
				
				$ret->status = 200;
				$ret->data = "操作成功";
				echo json_encode ( $ret );
				return;
			} else {
				$ret->status = 500;
				$ret->data = "操作失败";
				echo json_encode ( $ret );
				return;
			}
		}
	}
	
	/**
	 *
	 * @see ajax 删除部门
	 */
	function actionAjax_depart_delete() {
		$ret = new stdClass ();
		$ret->status = 500;
		/*
		 * if (Yii::app ()->user->isGuest) {
		 * $ret->data = "请登录";
		 * echo json_encode ( $ret );
		 * Yii::app ()->end ();
		 * }
		 */
		if (isset ( $_POST ['id'] ) && is_numeric ( $_POST ['id'] )) {
			$id = $_POST ['id'];
			$service = new SysDepartEntity ();
			$result = $service->delete_bogus ( $id );
			if ($result) {
				$ret->status = 200;
				$ret->data = "操作成功";
			} else {
				$ret->data = "操作失败";
			}
		}
		echo json_encode ( $ret );
	}
	/**
	 *
	 * @see 设置权限
	 */
	function actionAjax_set_power() {
		$ret = new stdClass ();
		$ret->status = 401;
		$id = $_POST ['id'];
		$power = $_POST ['power'];
		$service = new SysAdminEntity ();
		$result = $service->update ( array (
				'sys_role' => $power 
		), array (
				'id' => $id 
		) );
		if ($result) {
			$ret->status = 200;
			$ret->data = "操作成功";
		} else {
			$ret->status = 500;
			$ret->data = "操作失败";
		}
		echo json_encode ( $ret );
	}
}
